package com.zzx.test.reflection;

import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

/**
 * @ClassName: ReflectionTest3
 * @Description: 反射爆破测试类
 * @Author zhouzhixin
 * @Date 2021/12/3
 * @Version 1.0
 */
public class ReflectionTest3 {

    @Test
    public void test1() {
        try {
            Class<?> aClass = Class.forName("com.zzx.test.entity.Person");
            Constructor<?> constructor1 = aClass.getDeclaredConstructor(int.class, String.class, int.class);
            constructor1.setAccessible(true);
            Object o = constructor1.newInstance(10, "", 10);
            System.out.println(o);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test2() {
        try {
            Class<?> aClass = Class.forName("com.zzx.test.entity.Person");
            Object o = aClass.newInstance();
            Field hobby = aClass.getDeclaredField("hobby");
            hobby.setAccessible(true);
            hobby.set(null, "aaa");
            System.out.println(o);
            System.out.println(hobby.get(o));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
